package com.yaohe.common;

import java.text.SimpleDateFormat;
import java.util.Date;


import com.google.protobuf.ByteString;
import com.yaohe.common.key.CouponId;
import com.yaohe.common.key.StoreId;
import com.yaohe.consumer.util.Point;

public class Coupon {
	
	public CouponId id;					//Coupon id, unique
	public StoreId storeId;				//id of the store that issued this coupon
	
	public String description; 		//coupon description. For this version, all information of this coupon is in here
	
	public Date begin;				//begin time of the coupon
	public Date end;				//We dont use duration, but you can easily convert end and begin to a duration
	
	public int amount;				//Positive: amount ceiling; Negative : amount floor; Zero : no limit
	
	public int useInMinutes;		//Once applied, use in useInSecs;
	public int picID;				//
	
	public int style;
	/*
	 * All the fields below are not included in the database table COUPONS
	 * Some are acquired from other tables to accelerate querying, and some
	 * are dynamic information about this coupon 
	 */
	
	public String storeName;		//Sometimes we dont want to send the whole info of store along with coupon to 
									//user's device, but we simply send the name of the store to the user
	public String storeAddress;		//
	public Point location;			//the location of the store that issued this coupon
	
	public int amountRemaining;		
	
	/*
	 *We do not allow user to manually instantiate a Coupon:
	 *They may forget to set some fields, or
	 *they may erroneously set some fields, such as id(id cannot be set manually when issuing new coupon)
	 */
	private Coupon (){}
	
	
	public static Coupon buildCoupon(CouponId id , StoreId storeId, String description, Date begin, Date end, int amount, int remaining, int useInMinutes, int style) throws Exception
	{
		Coupon c = new Coupon();
		c.id= id;
		
		c.storeId = storeId;

		c.description = description;
		c.begin = begin;
		c.end= end;
		c.amount = amount;
		
		c.useInMinutes = useInMinutes;
		c.picID = id.getIntValue();
		c.style = style;
		c.amountRemaining = remaining;
		
		return c;
	}
	
	public static Coupon buildCouponWithStoreInfo(CouponId id , StoreId storeId, String description, Date begin, Date end, int amount, int remaining, int useInMinutes, int style, String name, String addr) throws Exception
	{
		Coupon c = new Coupon();
		c.id= id;
		
		c.storeId = storeId;

		c.description = description;
		c.begin = begin;
		c.end= end;
		c.amount = amount;
		
		c.useInMinutes = useInMinutes;
		c.picID = id.getIntValue();
		c.style = style;
		c.amountRemaining = remaining;
		
		c.storeName = name;
		c.storeAddress = addr;
		
		return c;
	}
	
	public String toString(){
		 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");  
		StringBuffer sBuffer = new StringBuffer(200);
		sBuffer.append("-{Coupon:: ");
		sBuffer.append("ID="+id+", ");
		sBuffer.append("STORE=("+storeId.toString()+", "+storeName+"), ");
		sBuffer.append("DESCRIPTION: "+description+", ");
		sBuffer.append("BEGIN@ "+df.format(begin)+", ");
		sBuffer.append("END@"+df.format(end)+", ");
		sBuffer.append("AMOUNT="+amount+", ");
		sBuffer.append("USE IN MINUTES ="+useInMinutes+", ");
		sBuffer.append("LOCATON=("+location.getLat()+", "+location.getLng()+")");
		sBuffer.append("}-");
		return sBuffer.toString();
		
	}
	
}
